//========================== 首页横幅自动切换
var BG = document.querySelector(".BG")
var temp = 2;
function changeBG(){
    if(temp>3){
        temp = 1;
    }
    BG.src="images/banner"+temp+".png"
    temp++;
}
setInterval(changeBG,3000)

//=============================导航栏置顶
var nav = document.querySelector(".page-nav")
var navHeight = nav.scrollHeight
var headerHeight = document.querySelector(".index-header").scrollHeight
function fixedNav(){
    //获取页面被卷起的高度即滚动条隐藏的高度
    var top_distance = document.documentElement.scrollTop
    if(top_distance>(headerHeight-navHeight)){
        nav.style.position = "fixed";
        nav.style.top = 0;
        nav.style.backgroundColor = "white";
    }else{
        nav.style.position = "static";
    }
}
//当滑动条滑动时调用该方法
$(document).scroll(function(){
    fixedNav()
})

//===================================秒杀提示部分
//===点击跳转选项卡
//10:00
function changePro1(){
        var value = $(".startTime").text();
        var strTime = value.substring(0,5);
        $.post("spikeProduct",{time:strTime},function (data) {
            proData = data;
            console.log(data);
            var token = localStorage.getItem("token");
            var product = $(".product");
            for (var i=0;i<product.length;i++){
                $(".phone").attr("src",data.productMainImg);
                $(".product").find("i").text(data.productName);
                var text = $(".price").children().eq(1).text(data.spikeProductPrice);
                //将proIMG的href路径获取到拼接上id
                $(".pro-img").attr("href","spikeProduct/detail?proId="+data.spikeProductId);
                $(".buyBtn").attr("href","spikeProduct/goConfirm?proId="+data.spikeProductId+"&token="+token);
            }
        })
}
//12:00
function changePro2(){
    var value = $(".startTime").text();
    var strTime = value.substring(5,10);
    $.post("spikeProduct",{time:strTime},function (data) {
        console.log(data);
        var product = $(".product");
        for (var i=0;i<product.length;i++){
            $(".phone").attr("src",data.productMainImg);
            $(".product").find("i").text(data.productName);
            var text = $(".price").children().eq(1).text(data.spikeProductPrice);
            //将proIMG的href路径获取到拼接上id
            $(".pro-img").attr("href","spikeProduct/detail?proId="+data.spikeProductId);
            $(".buyBtn").attr("href","spikeProduct/goConfirm?proId="+data.spikeProductId+"&token="+token);
        }
    })
}
//14:00
function changePro3(){
    var value = $(".startTime").text();
    var strTime = value.substring(10,15);
    $.post("spikeProduct",{time:strTime},function (data) {
        console.log(data);
        var product = $(".product");
        for (var i=0;i<product.length;i++){
            $(".phone").attr("src",data.productMainImg);
            $(".product").find("i").text(data.productName);
            var text = $(".price").children().eq(1).text(data.spikeProductPrice);
            //将proIMG的href路径获取到拼接上id
            $(".pro-img").attr("href","spikeProduct/detail?proId="+data.spikeProductId);
            $(".buyBtn").attr("href","spikeProduct/goConfirm?proId="+data.spikeProductId+"&token="+token);
        }
    })
}
//16:00
function changePro4(){
    var value = $(".startTime").text();
    var strTime = value.substring(15,20);
    $.post("spikeProduct",{time:strTime},function (data) {
        console.log(data);
        var product = $(".product");
        for (var i=0;i<product.length;i++){
            $(".phone").attr("src",data.productMainImg);
            $(".product").find("i").text(data.productName);
            var text = $(".price").children().eq(1).text(data.spikeProductPrice);
            //将proIMG的href路径获取到拼接上id
            $(".pro-img").attr("href","spikeProduct/detail?proId="+data.spikeProductId);
            console.log(data);
            $(".buyBtn").attr("href","spikeProduct/goConfirm?proId="+data.spikeProductId+"&token="+token);
        }
    })
}
//18:00
function changePro5(){
    var value = $(".startTime").text();
    var strTime = value.substring(20,25);
    $.post("spikeProduct",{time:strTime},function (data) {
        console.log(data);
        var product = $(".product");
        for (var i=0;i<product.length;i++){
            $(".phone").attr("src",data.productMainImg);
            $(".product").find("i").text(data.productName);
            var text = $(".price").children().eq(1).text(data.spikeProductPrice);
            //将proIMG的href路径获取到拼接上id
            $(".pro-img").attr("href","spikeProduct/detail?proId="+data.spikeProductId);
            $(".buyBtn").attr("href","spikeProduct/goConfirm?proId="+data.spikeProductId+"&token="+token);
        }
    })
}
// =======固定
var seckillTip = document.querySelector(".seckill-tip")
var seckillTipHeight = seckillTip.scrollHeight
//高度
var sumHeight = document.querySelector(".index-header").scrollHeight +
                document.querySelector(".banner").scrollHeight
function fixedTip(){
    //获取页面被卷起的高度即滚动条隐藏的高度
    var top_distance = document.documentElement.scrollTop
    if(top_distance>sumHeight-10){
        seckillTip.style.position = "fixed";
        seckillTip.style.top = "15px";
    }else{
        seckillTip.style.position = "static";
    }
}
//当滑动条滑动时调用该方法
$(document).scroll(function(){
    fixedTip()
})
//倒计时
function countTime(selected) {
    //获取当前时间  
    var date = new Date();
    //获取当前日期的第几天
    var dayOfDate = date.getDate();
    //获取月份
    var monthOfDate = date.getMonth()+1;
    //获取年份
    var yearOfDate = date.getFullYear();
    //获取当前时间的 时
    var nowHour = date.getHours();
    //获取当前时间的秒
    var second = date.getSeconds();
    //获取当前时间(毫秒)
    var now = date.getTime();
    //设置截止时间
    var tempHour = selected.children[0].children[0].innerText.substring(0,2)
    var hour = parseInt(tempHour)+2;
    var str=yearOfDate+"/"+monthOfDate+"/"+dayOfDate+" "+hour+":00:00";
    var endDate = new Date(str); 
    var end = endDate.getTime();
    //时间差  
    var leftTime = end-now;
    //定义变量 d,h,m,s保存倒计时的时间  
    var d,h,m,s;  
    if (leftTime>=0) {  
        d = Math.floor(leftTime/1000/60/60/24);  
        h = Math.floor(leftTime/1000/60/60%24);
        m = Math.floor(leftTime/1000/60%60);  
        s = Math.floor(leftTime/1000%60);                     
    }
    if(h==undefined){
        selected.children[0].children[1].innerText = "已结束"
        selected.children[0].children[2].innerHTML = ""
    }else if(h>=0 && nowHour<tempHour){
        selected.children[0].children[1].innerText = "即将开始"
        // selected.className = "tip selected";
        // document.querySelector(".tip.selected .timetip").innerHTML = "距离开始："
        // document.querySelector(".tip.selected ._h").innerHTML = h+"时";
        // document.querySelector(".tip.selected ._m").innerHTML = m+"分";
        // document.querySelector(".tip.selected ._s").innerHTML = s+"秒";
    }else {
        selected.children[0].children[1].innerText = "正在进行"
        selected.className = "tip selected";
        document.querySelector(".tip.selected .timetip").innerHTML = "距离结束："
        document.querySelector(".tip.selected ._h").innerHTML = h+"时";
        document.querySelector(".tip.selected ._m").innerHTML = m+"分";
        document.querySelector(".tip.selected ._s").innerHTML = s+"秒";
    }
    // document.getElementById("_d").innerHTML = d+"天";

}
function allTimeCount(){
    var tipArr = document.querySelectorAll(".tip")
    for (var i=0;i<tipArr.length;i++){
        var selected = tipArr[i]
        countTime(selected)
    }
}
//递归每秒调用countTime方法，显示动态时间效果 
setInterval(allTimeCount,1000);
// ==================================商品展示页面
//进度条
var haveSaledArr = document.querySelectorAll(".haveSaled")
for(var i=0;i<haveSaledArr.length;i++){
    var saledArr = document.querySelectorAll(".saled")
    var childArr = saledArr[i].children
    var saled = childArr[1].innerText
    var saledNum = saled.substring(0,saled.length-1);
    haveSaledArr[i].style.width = saledNum+"%"
}


